﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace CRM_Web.Controllers
{
    public class MouduleController : ApiController
    {
        // GET: api/Moudule
        /// <summary>
        /// 请求树形控件数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult getmoudulelist()
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                var AuthorizeInfo = db.Modules.Where(p => true);
                var tempdata = from a in AuthorizeInfo
                               where a.ParentId == 0
                               select new
                               {
                                   Id = a.Id,
                                   Name = a.Name,
                                   ParentId = a.ParentId,
                                   Path = a.Path,
                                   Weight = a.Weight,
                                   children = from t in AuthorizeInfo
                                              where t.ParentId == a.Id
                                              select new
                                              {
                                                  Id = t.Id,
                                                  Name = t.Name,
                                                  ParentId = t.ParentId,
                                                  Path = t.Path,
                                                  Weight = t.Weight
                                              }
                               };
                return Json(new { data = tempdata.ToList() });
            }
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>
        public IHttpActionResult editmoudules([FromBody] Modules module)
        {
            using (CRMDBEntities db = new CRMDBEntities())

            {
                var themoudu = db.Modules.FirstOrDefault(x => x.Id == module.Id);

                themoudu.Name = module.Name;

                themoudu.Path = module.Path;

                themoudu.Weight = module.Weight;

                themoudu.ParentId = module.ParentId;

                int n = db.SaveChanges();

                if (n > 0)

                {
                    return Json(new { code = 0, msg = "保存成功" });
                }
                else
                {
                   return Json(new { code = 0, msg = "保存失败" });
                }
            }
        }


        /// <summary>
        ///  删除模块
        /// </summary>
        /// <param name="mou"></param>
        /// <returns></returns>
        public IHttpActionResult delmoudules([FromBody] Modules mou)
        {
            using(CRMDBEntities db=new CRMDBEntities())
            {
                var moduleId = db.RoleModules.Where(p => p.ModuleId == mou.Id).ToList();
                var list = db.Modules.SingleOrDefault(p => p.Id == mou.Id);
                if (moduleId.Count() != 0)
                {
                    return Json(new { code = -1, msg = "已有角色使用该模块，不能删除！" });
                }
                else
                {
                    if (list.ParentId != 0)
                    {
                        db.Modules.Remove(list);
                        int n = db.SaveChanges();
                        if (n > 0)
                        {
                            return Json(new { code = 0, msg = "删除成功！" });
                        }
                        else
                        {
                            return Json(new { code = -1, msg = "删除失败！" });
                        }
                    }
                    else
                    {
                        var list2 = db.Modules.Where(p => p.ParentId == list.Id).ToList();
                        if (list2.Count() != 0)
                        {
                            return Json(new { code = -1, msg = "该模块下有子模块，不能删除" });
                        }
                        else
                        {
                            db.Modules.Remove(list);
                            int n = db.SaveChanges();
                            if (n > 0)
                            {
                                return Json(new { code = 0, msg = "删除成功！" });
                            }
                            else
                            {
                                return Json(new { code = -1, msg = "删除失败！" });
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 新增模块
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>
        public IHttpActionResult addmoudules([FromBody] Modules module)

        {

            using (CRMDBEntities db = new CRMDBEntities())

            {

                var themoudu = db.Modules.FirstOrDefault(x => x.Name == module.Name);

                if (themoudu != null)

                {

                    return Json(new { code = -1, msg = "已存在" });



                }

                else
                {

                    db.Modules.Add(module);

                    int n = db.SaveChanges();

                    if (n > 0)

                    {



                        return Json(new { code = 0, msg = "添加成功" });



                    }

                    else

                    {

                        return Json(new { code = -1, msg = "添加失败" });



                    }



                }



            }

        }

    }
}
